package view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import controller.BaralhoController;

public class JBarraMenu extends JMenu {

	private JMenuBar barra;
	private JMenu menu;
	private JMenu tabuleiroDoBaralho;
	private JMenuItem quatroPorQuatro;
	private JMenuItem seisPorSeis;
	private JMenuItem oitoPorOito;
	private JMenuItem sair;
	private Container container;
	private JFrame frame;
	private JBaralhoPanel baralho;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public JBarraMenu(JFrame frame){
		this.frame = frame;
		this.container = frame.getContentPane();
		barra = new JMenuBar();
		menu = new JMenu("Menu");
		menu.setMnemonic('M');

		tabuleiroDoBaralho = new JMenu("Tabuleiro");
		tabuleiroDoBaralho.setMnemonic('T');
		menu.add(tabuleiroDoBaralho);

		quatroPorQuatro = new JMenuItem(" 4 x 4 ");
		quatroPorQuatro.setMnemonic('4');

		seisPorSeis = new JMenuItem(" 6 x 6 ");
		seisPorSeis.setMnemonic('6');

		oitoPorOito = new JMenuItem(" 8 x 8 ");
		oitoPorOito.setMnemonic('8');

		tabuleiroDoBaralho.add(quatroPorQuatro);
		tabuleiroDoBaralho.add(seisPorSeis);
		tabuleiroDoBaralho.add(oitoPorOito);

		sair = new JMenuItem("Sair");
		sair.setMnemonic('S');

		barra.add(menu);
		menu.add(sair);

		linkListeners();
	}

	public JMenuBar getBarra() {
		return this.barra;
	}

	private void linkListeners(){
		sair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evento) {
				System.exit(0);
			}
		});

		quatroPorQuatro.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
					constroiTabuleiro(8);
			}
		});
		
		seisPorSeis.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
					constroiTabuleiro(18);
			}
		});
		
		oitoPorOito.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
					constroiTabuleiro(32);
					
			}
		});

	}

	private void constroiTabuleiro(int numeroDepares) {
		if(baralho != null){
			container.remove(baralho);
		}
		baralho = new JBaralhoPanel(BaralhoController.baralhoButtonCompleto(numeroDepares));
		container.add(baralho, BorderLayout.CENTER);
		switch (numeroDepares) {
		case 8:
			frame.setSize(400, 400);
			break;
		case 18:
			frame.setSize(550, 550);
			break;
		case 32:
			frame.setSize(800, 800);
			break;
		default:
			frame.setSize(550, 550);
			break;
		}
		frame.validate();
		container.validate();
	}

}
